package com.haoshuang.zhouzhoubang.ui.page.data.cate

import android.os.Bundle
import com.blankj.utilcode.util.ScreenUtils
import com.haoshuang.zhouzhoubang.databinding.FragmentCategoryStatisticsBinding
import com.haoshuang.zhouzhoubang.model.bean.MemberBean
import com.haoshuang.zhouzhoubang.model.bean.StoreServiceSkillBean
import com.haoshuang.zhouzhoubang.ui.dialog.ChooseDateRangeDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible

/**
 *  
 */
class CategoryStatisticsFragment : BaseVMFragment<CategoryStatisticsViewModel, FragmentCategoryStatisticsBinding>() {

    companion object {
        fun getInstance(): CategoryStatisticsFragment {
            val fragment = CategoryStatisticsFragment()
            fragment.arguments = Bundle().apply {

            }
            return fragment
        }
    }

    /**
     * 1: 今天, 2: 本周, 3: 本月, 4: 本年 0: 自定义
     */
    private var type = 1

    private var start_time = ""
    private var end_time = ""

    // 成员id
    private var child_id = ""
    private val memberList = arrayListOf<MemberBean>()

    // 服务id
    private var service_id = ""
    private val serviceList = arrayListOf<StoreServiceSkillBean>()

    override fun initView(savedInstanceState: Bundle?) {
        binding.rgTime.setOnCheckedChangeListener { group, checkedId ->
            binding.tvCustomTime.gone()
            start_time = ""
            end_time = ""
            when (checkedId) {
                binding.rbToday.id -> {
                    if (binding.rbToday.isChecked) {
                        type = 1
                        vm.getMemberStatistical(type, start_time, end_time, child_id)
                        vm.getServiceStatistical(type, start_time, end_time, service_id)
                    }
                }
                binding.rbCurrentWeek.id -> {
                    if (binding.rbCurrentWeek.isChecked) {
                        type = 2
                        vm.getMemberStatistical(type, start_time, end_time, child_id)
                        vm.getServiceStatistical(type, start_time, end_time, service_id)
                    }
                }
                binding.rbCurrentMonth.id -> {
                    if (binding.rbCurrentMonth.isChecked) {
                        type = 3
                        vm.getMemberStatistical(type, start_time, end_time, child_id)
                        vm.getServiceStatistical(type, start_time, end_time, service_id)
                    }
                }
                binding.rbCurrentYear.id -> {
                    if (binding.rbCurrentYear.isChecked) {
                        type = 4
                        vm.getMemberStatistical(type, start_time, end_time, child_id)
                        vm.getServiceStatistical(type, start_time, end_time, service_id)
                    }
                }
                binding.rbCustomTime.id -> {
                    if (binding.rbCustomTime.isChecked) {
                        type = 0
                        start_time = ""
                        end_time = ""
                        binding.tvCustomTime.visible()
                        binding.tvCustomTime.text = "请选择查询时间段"
                        XPopup.Builder(mActivity)
                            .asCustom(ChooseDateRangeDialog(mActivity){ start_date, end_date ->
                                start_time = start_date
                                end_time = end_date
                                binding.tvCustomTime.text = start_date + " ~ " + end_date
                                vm.getMemberStatistical(type, start_time, end_time, child_id)
                                vm.getServiceStatistical(type, start_time, end_time, service_id)
                            })
                            .show()
                    }else{
                        binding.tvCustomTime.gone()
                    }
                }
            }
        }

        binding.tvCustomTime.click {
            XPopup.Builder(mActivity)
                .asCustom(ChooseDateRangeDialog(mActivity){ start_date, end_date ->
                    start_time = start_date
                    end_time = end_date
                    binding.tvCustomTime.text = start_date + " ~ " + end_date
                    vm.getMemberStatistical(type, start_time, end_time, child_id)
                    vm.getServiceStatistical(type, start_time, end_time, service_id)
                })
                .show()
        }

        binding.tvMember.clickWithTrigger {
            // 切换成员
            if (memberList.isEmpty()) {
                toast("没有成员信息, 快去添加吧!")
                return@clickWithTrigger
            }
            val memberStrList = arrayListOf("全部")
            memberList.forEach {
                memberStrList.add(it.user_name?:"")
            }
            XPopup.Builder(mActivity)
                .maxHeight(ScreenUtils.getScreenHeight() / 2)
                .asBottomList("选择成员", memberStrList.toTypedArray()){position, text ->
                    child_id = if (position == 0) "" else memberList[position-1].uid.toString()
                    binding.tvMember.text = text
                    vm.getMemberStatistical(type, start_time, end_time, child_id)
                }
                .show()
        }
        binding.tvService.clickWithTrigger {
            // 切换服务
            if (serviceList.isEmpty()) {
                toast("没有技能信息, 快去添加吧!")
                return@clickWithTrigger
            }
            val serviceStrList = arrayListOf("全部")
            serviceList.forEach {
                serviceStrList.add(it.cate_name?:"")
            }
            XPopup.Builder(mActivity)
                .maxHeight(ScreenUtils.getScreenHeight() / 2)
                .asBottomList("选择服务", serviceStrList.toTypedArray()){position, text ->
                    service_id = if (position == 0) "" else serviceList[position-1].id.toString()
                    binding.tvService.text = text
                    vm.getServiceStatistical(type, start_time, end_time, service_id)
                }
                .show()
        }
    }

    override fun initData() {

    }

    override fun lazyLoadData() {
        vm.getStoreTeamMember()
        vm.getStoreServiceSkill()
        vm.getServiceStatistical(type, start_time, end_time, service_id)
        vm.getMemberStatistical(type, start_time, end_time, child_id)
    }

    override fun startObserve() {
        vm.getStoreTeamMemberData.observeState(this){
            onError { code, msg ->
                memberList.clear()
            }
            onSuccess {
                memberList.clear()
                memberList.addAll(it?.data?: emptyList())
            }
        }
        vm.getStoreServiceSkillData.observeState(this){
            onError { code, msg ->
                serviceList.clear()
            }
            onSuccess {
                serviceList.clear()
                serviceList.addAll(it?.data?: emptyList())
            }
        }

        vm.getMemberStatisticalData.observeState(this){
            onError { code, msg ->
                binding.apply {
                    tvMemberServiceOrderValue.text = "0"
                    tvApplauseRateValue.text = "0"
                    tvOrderTimeoutValue.text = "0"
                    tvTotalRevenueValue.text = "0.0"
                }
            }
            onSuccess {
                binding.apply {
                    tvMemberServiceOrderValue.text = it?.data?.order_total
                    tvApplauseRateValue.text = it?.data?.good_radio?.replace("%","")
                    tvOrderTimeoutValue.text = it?.data?.overtime_order_count
                    tvTotalRevenueValue.text = it?.data?.child_total_income.toString()
                }
            }
        }

        vm.getServiceStatisticalData.observeState(this){
            onError { code, msg ->
                binding.apply {
                    tvServiceOrderValue.text = "0"
                    tvServiceApplauseRateValue.text = "0"
                    tvServiceErrorOrderValue.text = "0"
                    tvServiceTotalRevenueValue.text = "0.0"
                }
            }
            onSuccess {
                binding.apply {
                    tvServiceOrderValue.text = it?.data?.order_total
                    tvServiceApplauseRateValue.text = it?.data?.good_radio?.replace("%","")
                    tvServiceErrorOrderValue.text = it?.data?.abnormal_order_count
                    tvServiceTotalRevenueValue.text = it?.data?.total_income.toString()
                }
            }
        }

    }

}